package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper contentMapper;


    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);

    }

    @Override
    public Long saveOrUpdate(Travel travel) {


        //摘要处理
        String content = travel.getTravelContent().getContent();
        if (content.length()>200){
            travel.setSummary(content.substring(0,200));
        }else {
            travel.setSummary(content);
        }


        //最后更新时间
        travel.setLastUpdateTime(new Date());



        if (travel.getId()!=null){

            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            contentMapper.updateByPrimaryKey(travelContent);

        }else {
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            //同时更新内容表
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            contentMapper.insert(travelContent);

        }

        return travel.getId();
    }

    @Override
    public Travel get(Long id) {

        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> queryViewNumTop3(Long id) {
        return travelMapper.queryViewNumTop3(id);
    }

    @Override
    public List<Travel> list() {

        return travelMapper.selectAll();
    }
}
